<template>
  <div class="mod-config">
      <el-form inline ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
        <el-form-item label="查看单位" prop="unitId">
          <el-select v-model="dataForm.unitId">
            <el-option v-for="item in unitOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchChange">查询</el-button>
        </el-form-item>
      </el-form>

      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
          <el-table-column  label="操作">
            <template slot-scope="scope">
              <el-button v-if="permissions.eims_inTransitUnit_edit && ['3','7','8','9','10'].includes(scope.row.code)" type="text" icon="el-icon-edit" @click="editStatus(scope.row)">修改状态</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="code" label="编码"></el-table-column>
          <el-table-column prop="inTransitName" label="在途状态名称"></el-table-column>
          <el-table-column prop="status" label="状态" :formatter="formatter"></el-table-column>
        </el-table>
      </div>
  </div>
</template>

<script>
  import { mapGetters } from "vuex"
  import { inTransitUnitInsert, getAllInfo, editStatusById } from "@/api/eims/intransitunit"
  import { unitList } from "@/api/admin/unit"

  export default {
    data() {
      return {
        dataForm: {
          unitId: '',
        },
        unitOptions: [],
        dataList: [],
        searchShow: true,
        dataListLoading: false
      };
    },
    created() {
      this.getUnitList();
      this.dataForm.unitId = this.userInfo.unitId
      this.getDataList();
    },
    computed: {
      ...mapGetters(["permissions", "userInfo"]),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        getAllInfo(this.dataForm.unitId).then((response) => {
          if(response.data.code == '0'){
            this.dataList = response.data.data
            this.dataListLoading = false
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.getDataList();
      },
      //搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields();
        this.getDataList();
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList();
      },
      editStatus(data) {
        this.$confirm('是否确认修改' + data.inTransitName + '的状态', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return editStatusById(data.id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('修改成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
          // if(status == 1){this.$message.success('启用成功')}else{this.$message.success('停用成功')}
        })
      },
      formatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 0:
            return '停用'
            break
          case 1:
            return '启用'
            break
        }
      },
      //获取单位列表
      getUnitList() {
        unitList().then(response => {
          if(response.data.code == '0'){
            this.unitOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      }
    },
  };
</script>
